package org.iuniv.adb.patent.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.iuniv.adb.patent.common.Constants;

/**
 * Database adapter class for creating DB connection
 *
 * @author Phuc
 */
public class DBAdapter {

    private static Properties props = null;

    // load database connection information from configuration file
    static {
        InputStream iStream = null;
        try {
            if (props == null) {
                iStream = DBAdapter.class.getResourceAsStream(Constants.JDBC_PROPS_NAME);
                props = new Properties();
                props.load(iStream);
            }
        } catch (IOException e) {
            Logger.getLogger(DBAdapter.class.getName()).log(Level.SEVERE, null, e);
        } finally {
            if (iStream != null) {
                try {
                    iStream.close();
                } catch (IOException e) {
                    Logger.getLogger(DBAdapter.class.getName()).log(Level.SEVERE, null, e);
                }
                iStream = null;
            }
        }
    }

    /**
     * Method to establish database connection
     *
     * @return {@link Connection}
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
        Connection connection = null;

        try {
            if (props != null) {
                // get database connection inform from properties file
                String jdbcDriver = props.getProperty(Constants.JDBC_DRIVER_KEY);
                String jdbcUrl = props.getProperty(Constants.JDBC_URL_KEY);
                String jdbcUsername = props.getProperty(Constants.JDBC_USERNAME_KEY);
                String jdbcPassword = props.getProperty(Constants.JDBC_PASSWORD_KEY);

                Class.forName(jdbcDriver);
                connection = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DBAdapter.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        } catch (SQLException ex) {
            Logger.getLogger(DBAdapter.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        }
        return connection;
    }

    /**
     * Method to get database name from configuration file
     *
     * @return the active database name
     * @throws IOException
     */
    public static String getDBName() throws IOException {
        String dbName = null;
        if (props != null) {
            dbName = props.getProperty(Constants.JDBC_DBNAME);
        }
        return dbName;
    }
}
